package com.example.springboot_demo.controller;

import com.example.springboot_demo.domain.User;
import com.example.springboot_demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String loginUser(String username, String password, HttpServletRequest servletRequest, Model model){
        User user = userService.login(username, password);
        if (user != null){
            servletRequest.getSession().setAttribute("user",user);
            return "redirect:index";
        }else {
            model.addAttribute("error","用户名或密码错误");
            return "login";
        }
    }

}
